package com.example.dragon.xmf;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dragon.common.Common;
import com.example.dragon.dialog.MessageDialog;
import com.example.dragon.dialog.WaitDialog;
import com.example.dragon.untis.DateUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class ConfirmOrderActivity extends AppCompatActivity {
    private TextView mdateText,maddressText,
    mnameText,mtelText,mfeeText,mattenText;
    private WaitDialog mwaitDialog;
    String dats;
    private MessageDialog messageDialog;
    private LinearLayout mbackLinyout;
    private String address,name,tel,date,fee,matter,message,lng,lat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);
        initControls();
    }
    private void initControls(){
        mwaitDialog = new WaitDialog(this,"4");
        maddressText = (TextView)findViewById(R.id.subadressText);
        mattenText = (TextView)findViewById(R.id.subbzText);
        mdateText = (TextView)findViewById(R.id.subdateText);
        mfeeText = (TextView)findViewById(R.id.subfeeText);
        mnameText = (TextView)findViewById(R.id.subnameText);
        mtelText = (TextView)findViewById(R.id.subtelText);
        mbackLinyout = (LinearLayout)findViewById(R.id.backLayout) ;

        Intent intent =getIntent();
        address = intent.getStringExtra("address");
        name = intent.getStringExtra("name");
        tel = intent.getStringExtra("mobile");
        date = intent.getStringExtra("worktime");
        fee = intent.getStringExtra("fee");
        matter = intent.getStringExtra("matter");
        message = intent.getStringExtra("message");
        lng = intent.getStringExtra("lng");
        lat = intent.getStringExtra("lat");
        dats = DateUtils.getTime(date);
        Log.i("ress",DateUtils.getStrTime(dats));
        mdateText.setText(date);
        mtelText.setText(tel);

        maddressText.setText(address);
        mnameText.setText(name);
        mattenText.setText(matter);
        mfeeText.setText(fee);

        mbackLinyout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConfirmOrderActivity.this.finish();
            }
        });
    }
    public void SubOrder(View view){
        OkHttpUtils.post()
                        .url(Common.MIFENG_URL+"Orders&a=postOrder")
                        .addParams("token", MainActivity.TOKEN)
                        .addParams("worktime",dats+"")
                        .addParams("name",name)
                        .addParams("mobile",tel)
                        .addParams("address",address)
                        .addParams("fee",fee)
                        .addParams("matter",matter)
                        .addParams("message",message)
                        .addParams("lng",lng)
                        .addParams("lat",lat)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                mwaitDialog.dismiss();
                                Log.i("res",e.toString());
                            }
                            @Override
                            public void onResponse(String response, int id) {
                                Log.i("res",response);
                                mwaitDialog.dismiss();
                                try {
                                    JSONObject jsonObject = new JSONObject(response);
                                    int code  = jsonObject.getInt("code");
                                    if (code==200){
                                        MessageDialog(jsonObject.getString("message"),1);
                                    }else {
                                        MessageDialog(jsonObject.getString("message"),2);
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
    }

    public void MessageDialog( CharSequence message,final  int is){
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("");
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (is==1){
                    Intent intent = new Intent(ConfirmOrderActivity.this, MainActivity.class);
                    intent.putExtra("order","1");
                    startActivity(intent);
                }
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }
}
